#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
int m[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int rever(int num)
{
	int sum=0;
	while(num)
	{
		sum=sum*10+num%10;
		num/=10;
	}
	return sum;
}
int check(int x)
{
	if(x%4 || (x%100==0 && x%400!=0)) return 1;
	return 0;
}
int cab(int x)
{
	vector<int> v;
	while(x)
	{
		v.push_back(x%10);
		x/=10;
	}
	if(v[0]!=v[1])
	{
		if(v[0]==v[2] && v[2]==v[5] && v[5]==v[7])
		{
			if(v[1]==v[3] && v[3]==v[4] && v[4]==v[6]) return 1;
		}
	}
	return 0;
}
int main()
{
	int num=0;
    cin>>num;
    int t=num/10000;
    int ret=0;
	int ans1=0;  
	int ans2=0;
	for(int i=t;i<=9999;i++)
    {
    	ret=rever(i);
    	int mou=ret/100;
    	int day=ret%100;
    	if(mou>12 || mou<1) continue;
    	int f=0;
    	if(check(i))
    	{
    		m[2]--;
    		if(day<1 || day>m[mou]) f=1;
    		m[2]++;
		}
		else
		{
			if(day<1 || day>m[mou]) f=1;
		}
		if(f) continue;
		int v=i*10000+ret;
		if(v>num && ans1==0)
		{
			ans1=v;
		}
		if(cab(v) && ans2==0 && v>num)
		{
			ans2=v;
		}
		if(ans1!=0 && ans2!=0) break; 
    }
    cout<<ans1<<'\n'<<ans2;

	return 0;
}

